package com.llu.threadlocal;

/**
 * @author jackliu  Email:
 * @description:
 * @Version
 * @create 2024-11-12 22:06
 *
 * 需求：线程隔离
 *
 *     线程A：设置（变量1） 获取（变量1）
 *     线程B：设置（变量2）  获取（变量2）
 */
public class MyDemo01 {

    ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private String content;

    private String getContent() {
//        return content;
//        System.out.println(threadLocal.toString());
        return threadLocal.get();
    }

    private void setContent(String content) {
//         this.content = content;
        // 将content的内容放到当前线程
        threadLocal.set(content);
     }

    public static void main(String[] args) {
        MyDemo01 myDemo01 = new MyDemo01();

        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    myDemo01.setContent(Thread.currentThread().getName() + "的数据");
                    System.out.println("--------------");
                    System.out.println(Thread.currentThread().getName() + "====> " + myDemo01.getContent());
                }
            });
            thread.setName("线程" + i);
            thread.start();

        }
    }
}
